package com.new_wear.project.system.task.controller;

import java.util.List;

import com.new_wear.common.utils.security.ShiroUtils;
import com.new_wear.framework.aspectj.lang.annotation.Log;
import com.new_wear.framework.aspectj.lang.enums.BusinessType;
import com.new_wear.framework.web.controller.BaseController;
import com.new_wear.framework.web.domain.AjaxResult;
import com.new_wear.framework.web.page.TableDataInfo;
import com.new_wear.project.system.dept.domain.Dept;
import com.new_wear.project.system.dept.service.IDeptService;
import com.new_wear.project.system.task.domain.CstTask;
import com.new_wear.project.system.task.service.ICstTaskService;
import com.new_wear.project.system.taskdo.domain.CstTaskDo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 任务列表Controller
 * 
 * @author new_wear
 * @date 2020-01-09
 */
@Controller
@RequestMapping("/system/taskExternal")
public class taskExternalController extends BaseController
{
    private String prefix = "system/taskExternal";

    @Autowired
    private ICstTaskService cstTaskService;
    @Autowired
    private IDeptService deptService;

    @RequiresPermissions("system:taskExternal:view")
    @GetMapping()
    public String taskExternal()
    {
        return prefix + "/taskExternal";
    }

    /**
     * 查询任务列表列表
     */
    @RequiresPermissions("system:taskExternal:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CstTask cstTask)
    {
        startPage();
        cstTask.setIsPlan(2);
        List<CstTask> list = cstTaskService.selectCstTaskList(cstTask);
        if (list != null || list.size()>0){
            for (CstTask task:list) {
                Integer areaId = task.getAreaId();
                Dept dept = deptService.selectDeptById(areaId.longValue());//地区
                task.setDeptName(dept.getDeptName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 新增任务列表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存任务列表
     */
    @RequiresPermissions("system:taskExternal:add")
    @Log(title = "任务列表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CstTask cstTask)
    {
        cstTask.setIsPlan(1);
        Long deptId = ShiroUtils.getSysUser().getDeptId();
        cstTask.setAreaId(deptId.intValue());
        return toAjax(cstTaskService.insertCstTask(cstTask));
    }

    /**
     * 修改任务列表
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        CstTask taskExternal = cstTaskService.selectCstTaskById(id);
        mmap.put("taskExternal", taskExternal);
        return prefix + "/edit";
    }

    /**
     * 修改保存任务列表
     */
    @RequiresPermissions("system:taskExternal:edit")
    @Log(title = "任务列表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CstTask cstTask)
    {
        cstTask.setIsCheck(0);
        return toAjax(cstTaskService.updateCstTask(cstTask));
    }

    /**
     * 删除任务列表
     */
    @RequiresPermissions("system:taskExternal:remove")
    @Log(title = "任务列表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cstTaskService.deleteCstTaskByIds(ids));
    }

    /*审核页面*/
    @GetMapping("/toexamine/{id}")
    public String toexamine(@PathVariable("id") Integer id, ModelMap mmap)
    {
        CstTask taskExternal = cstTaskService.selectCstTaskById(id);
        mmap.put("taskExternal", taskExternal);
        return prefix + "/toexamine";
    }

    @RequiresPermissions("system:taskExternal:upToexamine")
    @Log(title = "任务列表", businessType = BusinessType.UPDATE)
    @PostMapping("/upToexamine")
    @ResponseBody
    public AjaxResult upToexamine(CstTask cstTask)
    {
        return toAjax(cstTaskService.updateCstTask(cstTask));
    }


    /*终审页面*/
    @GetMapping("/finalJudgment/{id}")
    public String finalJudgment(@PathVariable("id") Integer id, ModelMap mmap)
    {
        CstTask taskExternal = cstTaskService.selectCstTaskById(id);
        mmap.put("taskExternal", taskExternal);
        return prefix + "/finalJudgment";
    }

    @RequiresPermissions("system:taskExternal:upFinalJudgment")
    @Log(title = "任务列表", businessType = BusinessType.UPDATE)
    @PostMapping("/upFinalJudgment")
    @ResponseBody
    public AjaxResult upFinalJudgment(CstTask cstTask)
    {
        return toAjax(cstTaskService.updateCstTask(cstTask));
    }
}
